package com.xmuexp.e09.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.xmuexp.e09.util.*;
import com.xmuexp.e09.model.*;
import com.xmuexp.e09.service.*;
import com.xmuexp.e09.dao.*;
import com.xmuexp.e09.util.RedisUtil;
import java.util.ArrayList;
import java.util.List;
import com.xmuexp.e09.mapper.*;

import javax.servlet.http.HttpServletResponse;

@RestController
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    private GoodsDao goodsDao;
    @Autowired
    private HttpServletResponse httpServletResponse;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private GoodsMapper goodsMapper;
    @RequestMapping(value = "/goods", produces = "application/json;charset=UTF-8")

    @GetMapping("{id}")
    public Object getGoodsById(@PathVariable("id") Integer id) {
        ReturnObject<VoObject> returnObject =  goodsService.findById(id);
        ResponseCode code = returnObject.getCode();
        switch (code){
            case RESOURCE_ID_NOTEXIST:
                httpServletResponse.setStatus(HttpStatus.NOT_FOUND.value());
                return ResponseUtil.fail(returnObject.getCode(), returnObject.getErrmsg());
            case OK:
                GoodsRetVo goodsRetVo = (GoodsRetVo) returnObject.getData().createVo();
                return ResponseUtil.ok(goodsRetVo);
            default:
                return ResponseUtil.fail(code);
        }
    }
    @GetMapping("/goods2/{id}")
    public Object getGoodsById2(@PathVariable("id") Integer id) {
        ReturnObject<VoObject> returnObject =  goodsService.findById(id);
        ResponseCode code = returnObject.getCode();
        switch (code){
            case RESOURCE_ID_NOTEXIST:
                httpServletResponse.setStatus(HttpStatus.NOT_FOUND.value());
                return ResponseUtil.fail(returnObject.getCode(), returnObject.getErrmsg());
            case OK:
                GoodsRetVo goodsRetVo = (GoodsRetVo) returnObject.getData().createVo();
                return ResponseUtil.ok(goodsRetVo);
            default:
                return ResponseUtil.fail(code);
        }
    }

}
